// 🎯 S5 策略：S1 + S2 投票融合预测
const select32NumbersByS1 = require("../v1/select32NumbersByS1.js");
const select32NumbersByS2 = require("../v2/select32NumbersByS2.js");

// 所有号码 01 ~ 49
const allNumbers = Array.from({ length: 49 }, (_, i) =>
  (i + 1).toString().padStart(2, "0")
);

/**
 * @param {Array} temp - 历史窗口数据
 * @returns {Array<string>} - 最终选中的 32 个号码
 */
function select32NumbersByS5(temp) {
  const votes = {}; // 投票统计
  allNumbers.forEach((num) => (votes[num] = 0));

  // 获取 S1、S2 各自预测的号码
  const s1Result = select32NumbersByS1(temp);
  const s2Result = select32NumbersByS2(temp);

  // 对每个策略结果进行投票
  for (const num of s1Result) votes[num]++;
  for (const num of s2Result) votes[num]++;

  // 统计总票数后排序
  const finalSelected = Object.entries(votes)
    .sort((a, b) => {
      if (b[1] !== a[1]) return b[1] - a[1]; // 票数多的优先
      return a[0].localeCompare(b[0]); // 相同票数按号码升序
    })
    .slice(0, 32)
    .map(([num]) => num);

  return finalSelected;
}

module.exports = select32NumbersByS5;
